package com.xl.basic.controller;

import com.xl.basic.annotation.RoleValidated;
import com.xl.basic.entity.Result;
import com.xl.basic.entity.SPage;
import com.xl.basic.entity.UserInfo;
import com.xl.basic.entity.param.AddUserParam;
import com.xl.basic.entity.param.QueryUserParam;
import com.xl.basic.entity.param.UpdUserParam;
import com.xl.basic.entity.param.UpdUserPasswordParam;
import com.xl.basic.entity.result.UserResult;
import com.xl.basic.service.UserService;
import com.xl.basic.utils.LoginUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotBlank;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 钟勇
 * Create in 2023/9/5 18:17
 */
@Tag(name = "用户管理")
@RestController
@RequestMapping("/api/User")
@RequiredArgsConstructor
@RoleValidated
public class UserController {

    private final UserService userService;

    @Operation(summary = "新增用户")
    @PostMapping(value = "/addUser")
    public Result<String> addUser(@Validated AddUserParam param) {
        Result<UserInfo> userInfoRes = LoginUtils.getUserInfo();
        return userService.addUser(param, userInfoRes.getResult());
    }


    @Operation(summary = "编辑用户")
    @PostMapping(value = "/updUser")
    public Result<Object> updUser(@Validated UpdUserParam param) {
        Result<UserInfo> userInfoRes = LoginUtils.getUserInfo();
        return userService.updUser(param, userInfoRes.getResult());
    }

    @Operation(summary = "修改用户密码")
    @PostMapping(value = "/updUserPassword")
    public Result<Object> updUserPassword(@Validated UpdUserPasswordParam param) {
        Result<UserInfo> userInfoRes = LoginUtils.getUserInfo();
        return userService.updUserPassword(param, userInfoRes.getResult());
    }

    @Operation(summary = "禁用用户")
    @PostMapping(value = "/disableUser")
    public Result<Object> disableUser(@NotBlank(message = "userId不能为空") String userId) {
        Result<UserInfo> userInfoRes = LoginUtils.getUserInfo();
        return userService.disableUser(userId, userInfoRes.getResult());
    }

    @Operation(summary = "启用用户")
    @PostMapping(value = "/enabledUser")
    public Result<Object> enabledUser(@NotBlank(message = "userId不能为空") String userId) {
        Result<UserInfo> userInfoRes = LoginUtils.getUserInfo();
        return userService.enabledUser(userId, userInfoRes.getResult());
    }

    @Operation(summary = "查询用户列表")
    @PostMapping(value = "/queryUserList")
    public Result<List<UserResult>> queryUserList(@Validated QueryUserParam param) {
        return userService.queryUserList(param);
    }

    @Operation(summary = "查询用户分页")
    @PostMapping(value = "/queryUserPage")
    public Result<List<UserResult>> queryUserPage(@Validated QueryUserParam param, SPage sPage) {
        return userService.queryUserPage(param, sPage);
    }
}
